package com.sms.graduate.controller;

import com.sms.graduate.entity.SmsUser;
import com.sms.graduate.entity.SmsWork;
import com.sms.graduate.entity.dto.WorkDTO;
import com.sms.graduate.entity.vo.WorkTimeVO;
import com.sms.graduate.service.SmsUserService;
import com.sms.graduate.service.SmsWorkService;
import com.sms.graduate.utils.R;
import com.sms.graduate.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


/**
 * @Author:jc
 * @Date:2021/10/19 14:49
 * @Description:com.sms.graduate.controller
 */
@RestController
@Api(tags = "作业模块")
@RequestMapping("/work")
@CrossOrigin(origins = "*")
public class WorkController {

    @Autowired
    SmsUserService smsUserService;

    @Autowired
    SmsWorkService smsWorkService;

    @PostMapping("/fileUpload")
    @ApiOperation("学生上传作业")
    public R fileUpload(HttpServletRequest request,@RequestPart("file") MultipartFile file,Integer id, @RequestParam("subject") String subject, @RequestParam("workName") String workName){
        try {
                String userCode = TokenUtils.getUserCode(request);
                SmsUser smsUser = smsUserService.selectUserByCode(userCode);
                String originalFilename = file.getOriginalFilename();
                int i = originalFilename.lastIndexOf(".");
                String substring = originalFilename.substring(i);
                String excelName = smsUser.getUsername()+userCode+substring;
                String outUrl = "E:\\work\\"+smsUser.getUserClass()+"\\"+subject+"\\"+workName+"\\";
                File file1 = new File(outUrl);
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                String url = outUrl+excelName;
                BufferedInputStream bufferedInputStream = new BufferedInputStream(file.getInputStream());
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(url));
                int len = 0;
                byte[] bytes = new byte[1024];
                while((len = bufferedInputStream.read(bytes)) != -1){
                    bufferedOutputStream.write(bytes,0,len);
                }
                bufferedOutputStream.close();
                bufferedInputStream.close();
               /* SmsSubmitwork smsSubmitwork = new SmsSubmitwork();
                smsSubmitwork.setSubmitTime(new Date());
                smsSubmitwork.setUsercode(userCode);
                smsSubmitwork*/
                return R.ok("作业上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/startAndEndTime")
    @ApiOperation("往前端返回作业的开始时间和结束时间")
    public R startAndEndTime(@RequestParam("id") Integer id){
        try {
            SmsWork smsWork = smsWorkService.selectWorkById(id);
            WorkTimeVO workTimeVO = new WorkTimeVO();
            workTimeVO.setEndTime(smsWork.getEndTime());
            workTimeVO.setStartTime(smsWork.getStartTime());
            return R.ok().put("data",workTimeVO);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @PostMapping("/createWork")
    @ApiOperation("老师发布作业")
    public R createWork(HttpServletRequest request, @RequestBody WorkDTO workDTO){
        try {
            String userCode = TokenUtils.getUserCode(request);
            SmsUser smsUser = smsUserService.selectUserByCode(userCode);
            String outUrl = "E:\\work\\"+smsUser.getUserClass()+"\\"+smsUser.getRemark()+"\\"+workDTO.getWorkName()+"\\";
            File file = new File(outUrl);
            if (!file.exists()){
                file.mkdirs();
            }
            SmsWork smsWork = smsWorkService.selectWorkByName(userCode, workDTO.getWorkName());
            if (smsWork == null){
                smsWorkService.addWork(workDTO,userCode);
                return R.ok("发布作业成功");
            }
            return R.ok("作业名重复,请更换作业名");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @GetMapping("/workList")
    @ApiOperation("作业列表")
    public R workList(){
       try {
         return R.ok("作业列表ok");
       }catch (Exception e){
           e.printStackTrace();
           return R.error();
       }
    }


    @GetMapping("/teacherWorkList")
    @ApiOperation("老师查看作业列表")
    public R teacherWorkList(HttpServletRequest request){
        try {
            String userCode = TokenUtils.getUserCode(request);
            List<SmsWork> smsWorks = smsWorkService.workList(userCode);
            return R.ok("作业列表查询成功").put("data",smsWorks);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @GetMapping("/studentWorkList")
    @ApiOperation("学生查询自己的作业")
    public R studentWorkList(HttpServletRequest request,@RequestParam(value = "remark",required = false) String remark){
         try {
             String userCode = TokenUtils.getUserCode(request);
             SmsUser smsUser = smsUserService.selectUserByCode(userCode);
             List<String> teacherCodeByUserInfo = smsUserService.getTeacherCodeByUserInfo(smsUser, remark);
             List<SmsWork> smsWorks = smsWorkService.studentWorkList(teacherCodeByUserInfo);
             return R.ok("作业列表查询成功").put("data",smsWorks);
         }catch (Exception e){
             e.printStackTrace();
             return R.error();
         }
    }




}
